package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dto.StuffDetailDto;
import com.health.db.model.GoodsDataUser;
import com.health.db.model.MedicalTreatRecordTemp;
import com.health.db.model.PatientList;
import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.db.model.RegisterOrderTemp;
import com.health.db.model.UserLIST;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.sm.IGoodsDataService;
import com.health.db.service.sm.IRongService;
import com.health.db.service.sm.ISMSSendService;
import com.health.db.service.user.IPatientListService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.GenerateHealthRecordOrderReq;
import com.health.mapp.bean.GenerateHealthRecordOrderResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *  健康咨询单生成接口
 * @author HiRoad
 * 
 */
public class GenerateHealthRecordOrderImpl implements ActionInterface {
	
	//service import
	private IRegisterOrderService registerOrderService  ;
	private IUserLISTService userLISTService  ;
	private IPatientListService patientListService  ;
	private IRongService rongService  ; 
	private IGoodsDataService goodsDataService ;
	private IStuffListService stuffListService  ;
	private  ISMSSendService sMSSendService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		GenerateHealthRecordOrderReq req = (GenerateHealthRecordOrderReq) reqBean; 
		GenerateHealthRecordOrderResp _resp = new GenerateHealthRecordOrderResp();
		String recordId = req.getHealthRecordId();
		if (StringUtils.isBlank(recordId)) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		else
		{
			GoodsDataUser goodsDataUser =goodsDataService.selectSimpleGoodsDataUser( Long.valueOf(recordId));
			
			RegisterOrderExample example = new RegisterOrderExample();
			
			example.setRegisterguideid(String.valueOf(9));
			example.setRefbillid(Long.valueOf(recordId));
			
			List<RegisterOrder> orderList =  registerOrderService.selectSimple(example);
			
			
			if (orderList.size()>0) {
				
				RegisterOrder orderTemp =orderList.get(0);
				
				//判断是否有咨询过未过期的订单
				if (orderTemp.getRunstatusid()<4) {
					
					RegisterOrderTemp returnOrderInfo = new RegisterOrderTemp();
					returnOrderInfo.setOrderId(String.valueOf(orderTemp.getRegisterid()));
					returnOrderInfo.setOrderState(String.valueOf(orderTemp.getRunstatusid()));
				    MedicalTreatRecordTemp returnPatRecord = new MedicalTreatRecordTemp();
				    returnPatRecord.setPatRecordId(String.valueOf(orderTemp.getRefbillid()));
				    returnOrderInfo.setPatRecord(returnPatRecord);
				    returnOrderInfo.setOrderFee((orderTemp.getRegisterTotal())==null?"0":String.valueOf(orderTemp.getRegisterTotal()));
					_resp.setOrderInfo(returnOrderInfo);
					return _resp;
				}else{
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3025);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3025_NOTE);
					return _resp;
				}
			}
			
			if (null==goodsDataUser) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
				return _resp;
			}
			
			//userId
			Long userId = goodsDataUser.getOwnerId();
			
			//RegisterOrder 包装
			RegisterOrder orderInfo = new RegisterOrder();
			
			//记录appid
			orderInfo.setWorkFlowStatus(header.getAppId());
			
			orderInfo.setRegisterguideid(String.valueOf(Constant.CONSTANTS_ORDER_CHANNEL_ID_9.intValue()));
			orderInfo.setRegisterNo(DateUtils.todayDate()+"-H0U"+userId+"-");
			orderInfo.setPatientid(goodsDataUser.getPatientId());
			orderInfo.setUserid(userId);
			orderInfo.setRegistertypeid(Constant.CONSTANTS_ORDER_TYPE_ID_1);
			orderInfo.setRegisterTypeName(Constant.CONSTANTS_ORDER_TYPE_DES_1);
			orderInfo.setUpdateUserid(userId);
			orderInfo.setRegisterDesc(Constant.CONSTANTS_ORDER_TYPE_DES_5+"相关问题咨询!");
			orderInfo.setOrderfeetypeid(Constant.CONSTANTS_ORDER_FREE_ID_0);
			orderInfo.setOrderTypeDesc(Constant.CONSTANTS_ORDER_TYPE_DES_5+","+Constant.CONSTANTS_ORDER_FREE_Map.get(Constant.CONSTANTS_ORDER_FREE_ID_0));
			orderInfo.setRefbillid(Long.valueOf(recordId));
			//查看用户表相关信息
			UserLIST userLIST = userLISTService.selectByUserId(userId);
			
			//用户下就诊人(宝宝)的就诊信息
			PatientList patient=patientListService.selectByPatientId(goodsDataUser.getPatientId());
			if(null!=patient)
			{
				orderInfo.setPatientName(patient.getPatientName());
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			
			orderInfo.setUserName(userLIST.getUserName());
			orderInfo.setUserContact(userLIST.getUserNo());
			//是否已支付
			orderInfo.setRegisterPayFlag(Constant.CONSTANTS_IS_PAYMENT_FALSE);
			
			//医生相关信息设置
			StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(goodsDataUser.getAccepterId());
			
			//医生个人信息
			orderInfo.setDepartmentid(stuffDetailDto.getDepartmentid());
			orderInfo.setDepartmentName(stuffDetailDto.getDepartmentName());
			orderInfo.setAccepterid(stuffDetailDto.getStuffid());
			orderInfo.setAccepterName(stuffDetailDto.getStuffName());
			orderInfo.setEntityid(stuffDetailDto.getEntityid());
			
			orderInfo.setRegisterTotal(new BigDecimal("0"));
			orderInfo.setRunStatus(Constant.CONSTANTS_ORDER_RUNSTATUS_DES_2);
			orderInfo.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_2);
			orderInfo.setGoodsid(Long.valueOf(Constant.CONSTANTS_ORDER_FREE_GOODSID));
			orderInfo.setRegisterPayoffTotal(new BigDecimal("0"));
			
			orderInfo.setGoodsQuantity(new BigDecimal("1"));
			Date currDateTime=DateUtils.getDatetime();
			
			//第一次生成订单，更新时间和创建时间一致
			orderInfo.setCreateDate(currDateTime);
			orderInfo.setUpdateDate(currDateTime);
 
			//返回参数
			RegisterOrderExample temp = registerOrderService.insertHealthRecordOrder(orderInfo);
			
			
			//包装返回参数 主要未订单号，订单状态
			RegisterOrderTemp returnOrderInfo = new RegisterOrderTemp();
			returnOrderInfo.setOrderId(String.valueOf(temp.getRegisterid()));
			returnOrderInfo.setOrderState(String.valueOf(orderInfo.getRunstatusid()));
		    MedicalTreatRecordTemp returnPatRecord = new MedicalTreatRecordTemp();
		    returnPatRecord.setPatRecordId(String.valueOf(orderInfo.getRefbillid()));
		    returnOrderInfo.setPatRecord(returnPatRecord);
		    returnOrderInfo.setOrderFee((orderInfo.getRegisterTotal())==null?"0":String.valueOf(orderInfo.getRegisterTotal()));
			_resp.setOrderInfo(returnOrderInfo);
			
			orderInfo.setRegisterid(temp.getRegisterid());
			
			String groupId = String.valueOf(temp.getRegisterid());
			String[] userIds = {String.valueOf(userId)};
			rongService.createGroup(userIds, groupId, groupId);
			rongService.PublishSystemOrderToDocMsg(orderInfo);
			sMSSendService.notice85785039(stuffDetailDto.getStuffName(), stuffDetailDto.getStuffNo());
		}
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IRegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}

	public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}

	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

	public IPatientListService getPatientListService() {
		return patientListService;
	}

	public void setPatientListService(IPatientListService patientListService) {
		this.patientListService = patientListService;
	}

	public IRongService getRongService() {
		return rongService;
	}

	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}

	public IGoodsDataService getGoodsDataService() {
		return goodsDataService;
	}

	public void setGoodsDataService(IGoodsDataService goodsDataService) {
		this.goodsDataService = goodsDataService;
	}

	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	public ISMSSendService getsMSSendService() {
		return sMSSendService;
	}

	public void setsMSSendService(ISMSSendService sMSSendService) {
		this.sMSSendService = sMSSendService;
	}
}
